//拼团首页
exports.bulk_purchase = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //几人可以成团
    var minCount = 0;
    

    //任务流创建
    async.waterfall([

            //1.拼团活动
            function (cb) {
                if(restfulParams.o_activity_type==0){
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_bulk_purchase_activity_info + "/oa_id/" + restfulParams.oa_id,
                            method: 'GET'
                        },
                        function (err, data) {

                            console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
                            if(data.data.length>0){

                                resultData.activityData = data.data[0];

                                minCount = resultData.activityData.bpac_min_count;

                            }else{
                                return  RES.response(res,false,"活动异常");
                            }
                            cb();
                        }
                    );

                }else if(restfulParams.o_activity_type==5){
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchDiscountsActivity + "/oa_id/" + restfulParams.oa_id,
                            method: 'GET'
                        },
                        function (err, data) {

                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }
                            if(data.data.length>0){

                                resultData.activityData = data.data[0];
                                minCount = [];
                            }else{
                                return  RES.response(res,false,"活动异常");
                            }
                            cb();
                        }
                    );

                }else{
                    resultData.activityData = [];
                    cb()
                }

            },

            //查询皮肤信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.searchActivitySkin +"/as_id/" + resultData.activityData.oa_skin_id,
                        method: 'GET',
                    },
                    function (err,data) {
                        if(err){

                            return cb(err,data);
                        }
                        resultData.skinData = data.data
                        cb();
                    }
                )
            },
            //查询音乐信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url:CORE_APIConfig.coreAPI.searchActivityMusic +"/am_id/" + resultData.activityData.oa_music_id,
                        method: 'GET',
                    },
                    function (err,data) {
                        if(err){

                            return cb(err,data);
                        }
                        resultData.musicData = data.data
                        cb();
                    }
                )
            },

            //查询拼团奖励
            /*
            function(cb){

                //判断是否有开团奖励
                if(resultData.activityData.bpac_builder_coupon_id == 0){
                    cb();
                }else{

                    //代金卷
                    if(resultData.activityData.bpac_builder_coupon_type == 0){

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+resultData.activityData.bpac_builder_coupon_id,
                                method: 'GET',

                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }

                                if(data.data !=''){
                                    resultData.activityData.bpacBuilderCouponName      = data.data[0].cc_name;
                                    resultData.activityData.bpacBuilderCouponStartTime = data.data[0].cc_start_time;
                                    resultData.activityData.bpacBuilderCouponEndTime   = data.data[0].cc_end_time;
                                    cb();
                                }else {
                                    cb();
                                }
                            }
                        );

                    //约课劵    
                    }else if(resultData.activityData.bpac_builder_coupon_type == 1){

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+resultData.activityData.bpac_builder_coupon_id,
                                method: 'GET',

                            },
                            function (err, data) {
                              
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }
                                if(data.data !='') {
                                    resultData.activityData.bpacBuilderCouponName       = data.data[0].cc_name;
                                    resultData.activityData.bpacBuilderCouponStartTime  = data.data[0].cc_start_time;
                                    resultData.activityData.bpacBuilderCouponEndTime    = data.data[0].cc_end_time;
                                    cb();
                                }else {
                                    cb();
                                }
                            }
                        );

                    //兑换卷    
                    }else if(resultData.activityData.bpac_builder_coupon_type == 2){
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_exchange_coupon+"/ec_id/"+resultData.activityData.bpac_builder_coupon_id,
                                method: 'GET',

                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }
                                if(data.data !='') {
                                    resultData.activityData.bpacBuilderCouponName       = data.data[0].ec_name;
                                    resultData.activityData.bpacBuilderCouponStartTime  = data.data[0].ec_start_time;
                                    resultData.activityData.bpacBuilderCouponEndTime    = data.data[0].ec_end_time;
                                    cb();
                                }else {
                                    cb();
                                }
                            }
                        );
                    }else{
                      cb();
                    }
                            
                }
            },
            

            function(cb){

                //判断是否有开团奖励
                if(resultData.activityData.bpac_participant_coupon_id == 0){

                    cb();

                }else{

                    //代金卷
                    if(resultData.activityData.bpac_participant_coupon_type == 0){

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+resultData.activityData.bpac_participant_coupon_id,
                                method: 'GET',

                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }

                                if(data.data !=''){
                                    resultData.activityData.bpacParticipantCouponName      = data.data[0].cc_name;
                                    resultData.activityData.bpacParticipantCouponStartTime = data.data[0].cc_start_time;
                                    resultData.activityData.bpacParticipantCouponEndTime   = data.data[0].cc_end_time;
                                    cb();
                                }else {
                                    cb();
                                }
                            }
                        );

                    //约课劵    
                    }else if(resultData.activityData.bpac_participant_coupon_type == 1){

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+resultData.activityData.bpac_participant_coupon_id,
                                method: 'GET',

                            },
                            function (err, data) {
                              
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }
                                if(data.data !='') {
                                    resultData.activityData.bpacParticipantCouponName       = data.data[0].cc_name;
                                    resultData.activityData.bpacParticipantCouponStartTime  = data.data[0].cc_start_time;
                                    resultData.activityData.bpacParticipantCouponEndTime    = data.data[0].cc_end_time;
                                    cb();
                                }else {
                                    cb();
                                }
                            }
                        );

                    //兑换卷    
                    }else if(resultData.activityData.bpac_participant_coupon_type == 2){
                        //console.log(123)
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchExchangeCoupon+"/ec_id/"+resultData.activityData.bpac_participant_coupon_id,
                                method: 'GET',

                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }
                                if(data.data !='') {
                                    resultData.activityData.bpacParticipantCouponName       = data.data[0].ec_name;
                                    resultData.activityData.bpacParticipantCouponStartTime  = data.data[0].ec_start_time;
                                    resultData.activityData.bpacParticipantCouponEndTime    = data.data[0].ec_end_time;
                                    cb();
                                }else {
                                    cb();
                                }
                            }
                        );
                    }else{
                        cb();
                    }

                }

            },
            */

            //拼团详情
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityContent + "/bpac_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {

                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if(resultData.activityData.oa_role){
                            resultData.activityContent = [];
                            resultData.activityContent[0] = 3;
                        }else{
                            resultData.activityContent = data.data;
                        }
                        cb();
                    }
                );

                

            },

            //获取机构信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + resultData.activityData.oa_oid,
                        method: 'GET',
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationData = data.data[0];

                        cb();
                    }
                );

            },

            //根据类型查询相关数据
            function(cb){

                if( resultData.activityData.bpac_type ==2 ){
                        
                    var oa_join_count            = parseInt(resultData.activityData.oa_join_count) + 1;
                    var bpac_participation_price = resultData.activityData.bpac_participation_price.split("|");
                    var bpac_min_count           = resultData.activityData.bpac_min_count.split("|");

                    var nowRange = 0;

                    var pArr = [];

                    //查询当前参与人数处于哪一个档位
                    for(var i = 0 ;i<bpac_min_count.length;i++){

                        var tempArr = bpac_min_count[i].split("-");

                        if(parseInt(oa_join_count) >= parseInt(tempArr[0]) && parseInt(oa_join_count) <= parseInt(tempArr[1])){

                            nowRange = i;

                        }

                        pArr.push(tempArr[1]);

                    }

                    resultData.activityData.bpac_min_count = pArr.join("|");

                    //获取该档位的价格
                    resultData.nowRangePrice = bpac_participation_price[nowRange];


                    cb();


                }else {
                    cb();
                }

            },
            
            //2.拼团活动评论
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.activity_comment_info + "/oac_oaid/"+restfulParams.oa_id+"/skip/0/limit/" + config.publicConfig.LIMIT+"/count/1",
                        method: 'GET',
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.commentCount = data.count;
                        resultData.commentData  = data.data;
                        cb();
                    }
                );

            },

            //2.开团用户
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/"+restfulParams.oa_id+"/bpau_fid/0/skip/0/limit/10/count/1/bpau_is_complete/0/o_pay_status/1",
                        method: 'POST',
                        data: {
                            fields: ["bpau_id","bpau_id","bpau_uid", "bpau_user_count", "u_nickname", "u_header_url","bpau_member_count","o_pay_status"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.joinUserCount = data.count;
                        resultData.joinUserData = data.data;
             
                        cb();
                    }
                );

            },

            //查询还差成团的人数
            function(cb){

                for(var i =0;i<resultData.joinUserData.length;i++){

                    var differCount  = resultData.joinUserData[i].bpau_member_count - resultData.joinUserData[i].bpau_user_count;

                    resultData.joinUserData[i].differCount = differCount;

                }

                cb();
                
            },

            //1.参与人数
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/"+restfulParams.oa_id + "/skip/0/limit/7/count/1",
                        method: 'POST',
                        data: {
                            fields: ["u_nickname", "u_header_url"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.completeBulkPurchaseCount = data.count;
                        resultData.completeBulkPurchaseData = data.data;
                        cb();
                    }
                );

            },

            //是否参与了该活动
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/"+restfulParams.oa_id + "/bpau_uid/" + restfulParams.u_id + "/bpau_is_complete/0",
                        method: 'POST',
                        data: {
                            fields: ["bpau_id","bpau_member_count","bpau_fid"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        if(data.data.length > 0){

                            resultData.jsJoin = true;
                            resultData.jsJoinGroup = data.data;
                        }else{
                            
                            resultData.jsJoin = false;
                        }

                        cb();
                    }
                );  

            }


        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}